package indi.zhifa.study2025.class001.eltest.logic.impl;

import indi.zhifa.core.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2025.class001.eltest.gen.item.po.GenItemEntity;
import indi.zhifa.study2025.class001.eltest.gen.item.service.IGenItemDbService;
import indi.zhifa.study2025.class001.eltest.logic.IItemService;
import indi.zhifa.study2025.class001.eltest.logic.entity.ItemRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class ItemServiceImpl implements IItemService {

    private final IGenItemDbService mGenItemDbService;

    @Override
    public GenItemEntity addItem(ItemRequest pItemEntity) {
        GenItemEntity itemEntity = DbDtoEntityUtil.createFromDto(pItemEntity, GenItemEntity.class);
        mGenItemDbService.save(itemEntity);
        return itemEntity;
    }

    @Override
    public GenItemEntity updateItem(Long pId, ItemRequest pItemEntity) {
        GenItemEntity itemEntity = mGenItemDbService.check(pId);
        GenItemEntity newItemEntity = DbDtoEntityUtil.editByDto(itemEntity,pItemEntity, GenItemEntity.class);
        mGenItemDbService.updateById(newItemEntity);
        return newItemEntity;
    }

    @Override
    public GenItemEntity info(Long pId) {
        GenItemEntity itemEntity = mGenItemDbService.check(pId);
        return itemEntity;
    }

    @Override
    public GenItemEntity deleteItem(Long pId) {
        GenItemEntity itemEntity = mGenItemDbService.check(pId);
        mGenItemDbService.deleteById(pId);
        return itemEntity;
    }
}
